package commons.base.httpserver;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

import commons.base.util.GzipUtil;

/**
 * spring mvc全局配置可以放在这里
 * @author yuan<cihang.yuan@happyelements.com>
 *
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = { WebConfig.class })
public class WebConfig extends WebMvcConfigurationSupport {

//	@Bean
//	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
//		RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
//		handlerMapping.setUseSuffixPatternMatch(false);
//		handlerMapping.setUseTrailingSlashMatch(false);
//		return handlerMapping;
//	}
//	
	@Bean
	public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
		RequestMappingHandlerAdapter handlerAdapter = super.requestMappingHandlerAdapter();

    	List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    	
    	// 使用FastJson做消息转换
    	FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4 = new FastJsonHttpMessageConverter4(){
    	    @Override
    	    public Object read(Type type, //
    	                       Class<?> contextClass, //
    	                       HttpInputMessage inputMessage //
    	    ) throws IOException, HttpMessageNotReadableException {
    	    	if(inputMessage.getHeaders().containsKey("contentCompress")){
//    	    		System.err.println("len1 = " + inputMessage.getHeaders().getContentLength());
    	    		byte[] rawData = new byte[(int)inputMessage.getHeaders().getContentLength()];
    	    		inputMessage.getBody().read(rawData);
    	    		return JSON.parseObject(GzipUtil.uncompress(rawData), type, getFastJsonConfig().getFeatures());
    	    	}
    	    	
    	    	return super.read(type, contextClass, inputMessage);
    	    }
    	    
    		@Override
    	    protected Object readInternal(Class<? extends Object> clazz, //
    	                                  HttpInputMessage inputMessage //
    	    ) throws IOException, HttpMessageNotReadableException {
    			if(inputMessage.getHeaders().containsKey("contentCompress")){
//    				System.err.println("len2 = " + inputMessage.getHeaders().getContentLength());
    	    		byte[] rawData = new byte[(int)inputMessage.getHeaders().getContentLength()];
    	    		inputMessage.getBody().read(rawData);
    	    		return JSON.parseObject(GzipUtil.uncompress(rawData), clazz, getFastJsonConfig().getFeatures());
    	    	}
    			
    	        return super.readInternal(clazz, inputMessage);
    		}
    	};
    	fastJsonHttpMessageConverter4.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
    	messageConverters.add(fastJsonHttpMessageConverter4);
    	
    	handlerAdapter.setMessageConverters(messageConverters);
    	
    	return handlerAdapter;
	}

}
